package com.junmoo.www.tool;

import com.alibaba.fastjson.JSONObject;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;
import java.nio.charset.StandardCharsets;


public class RespUtil {
    public static Mono<Void> writeMsg(ServerHttpResponse response, int status, Object data){
        response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
        response.setRawStatusCode(status);
        DataBuffer buffer = response.bufferFactory().wrap( JSONObject.toJSONString(data).getBytes(StandardCharsets.UTF_8) );
        return response.writeWith(Mono.just(buffer));
    }

    public static Mono<Void> writeMsg(ServerHttpResponse response,Object data){
        return writeMsg(response,200,data);
    }
}
